package com.woniu.mealsService.service.impl;

import com.woniu.mealsService.model.StoreFileUpload;
import com.woniu.mealsService.mapper.StoreFileUploadMapper;
import com.woniu.mealsService.service.StoreFileUploadService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.utils.AliyunOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.UUID;

/**
 * <p>
 * 商铺文件上传表 服务实现类
 * </p>
 *
 * @author jiang
 * @since 2024年12月09日
 */
@Service
public class StoreFileUploadServiceImpl extends ServiceImpl<StoreFileUploadMapper, StoreFileUpload> implements StoreFileUploadService {

    @Resource
    private AliyunOSSUtil aliyunOSSUtil;
    @Autowired
    private StoreFileUploadMapper storeFileUploadMapper;
    @Override
    public String upload(MultipartFile file, String userId) throws IOException {
        //进行文件上传
        InputStream inputStream = file.getInputStream();
        byte[] fileHeader = new byte[4];
        inputStream.read(fileHeader);
        if (!(AliyunOSSUtil.isJpg(fileHeader)||AliyunOSSUtil.isPng(fileHeader))) {
            throw new RuntimeException("文件格式不正确");
        }
        String suffix = StringUtils.getFilenameExtension(file.getOriginalFilename());
        String prefix = UUID.randomUUID().toString();
        String fileUrl = aliyunOSSUtil.uploadFile(file.getInputStream(), prefix + "." + suffix, "jiang115");
        //保存文件信息
        StoreFileUpload storeFileUpload = new StoreFileUpload();
        storeFileUpload.setFilePath(fileUrl);
        storeFileUpload.setFileName(file.getOriginalFilename());
        storeFileUpload.setFileSuffix(suffix);
        storeFileUpload.setSize(file.getSize());
        storeFileUpload.setCreateUser(userId);
        storeFileUpload.setCreateTime(LocalDateTime.now());
        save(storeFileUpload);
        return fileUrl;
    }

    @Override
    public void bindFileUploadId(Long fileUploadId, String file_path) {
        Integer count = storeFileUploadMapper.bindFileUploadId(fileUploadId, file_path);
        if (count != 1) {
            throw new RuntimeException("绑定文件上传id失败");
        }
    }

    @Override
    @Transactional
    public void updateFile(String fileUploadId, MultipartFile file, String userId) throws IOException {
        //查询旧文件路径
        StoreFileUpload oldFile = storeFileUploadMapper.searchStoreFileUploadById(Long.valueOf(fileUploadId));
        if (oldFile == null) {
            throw new RuntimeException("文件不存在");
        }
        String oldFileUrl = oldFile.getFilePath();
        //进行文件上传
        InputStream inputStream = file.getInputStream();
        byte[] fileHeader = new byte[4];
        inputStream.read(fileHeader);
        if (!(AliyunOSSUtil.isJpg(fileHeader)||AliyunOSSUtil.isPng(fileHeader))) {
            throw new RuntimeException("文件格式不正确");
        }
        String suffix = StringUtils.getFilenameExtension(file.getOriginalFilename());
        String prefix = UUID.randomUUID().toString();
        String fileUrl = aliyunOSSUtil.uploadFile(file.getInputStream(), prefix + "." + suffix, "jiang115");
        //保存文件信息
        StoreFileUpload storeFileUpload = new StoreFileUpload();
        storeFileUpload.setFileUploadId(fileUploadId);
        storeFileUpload.setFilePath(fileUrl);
        storeFileUpload.setFileName(file.getOriginalFilename());
        storeFileUpload.setFileSuffix(suffix);
        storeFileUpload.setSize(file.getSize());
        storeFileUpload.setUpdateUser(userId);
        storeFileUpload.setUpdateTime(LocalDateTime.now());
        storeFileUploadMapper.updateFile(storeFileUpload);
        //删除旧文件
        aliyunOSSUtil.deleteFile("jiang115", oldFileUrl);
    }
}
